/*
* @Author: ystlong
* @Date:   2018-05-12 10:21:41
* @Last Modified by:   long
* @Last Modified time: 2018-06-28 20:41:21
*/

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>

size_t get_executable_path( char* processdir,char* processname, size_t len)
{
    char* path_end;
    if(readlink("/proc/self/exe", processdir,len) <=0)
        return -1;
    path_end = strrchr(processdir, '/');
    if(path_end == NULL)
        return -1;
    ++path_end;
    strcpy(processname, path_end);
    *path_end = '\0';
    return (size_t)(path_end - processdir);
}

// {hold_start}

// $FN_NAME
// $ARG_LIST_DEF
// $ARG_NAME_LIST
// $IS_RETURN
// $RETURN_TYPE
$RETURN_TYPE (*real$FN_NAME)($ARG_LIST_DEF);
$RETURN_TYPE $FN_NAME($ARG_LIST_DEF)
{
    void *handle;
    char *error;
    // char path[PATH_MAX];
    // char processname[1024];
    // get_executable_path(path, processname, sizeof(path));
    // if (strcmp(processname, "armlmd") == 0) {
    //     puts("=========got armlmd: sleep 10s=====");
    //     int i;
    //     for(i=0;i<50000;i++)
    //         sleep(10);
    //     return;
    // }
    puts("-----$FN_NAME hold-----");
    handle = dlopen("libc.so.6", RTLD_LAZY);
    if ((error = dlerror()) != NULL){
        puts(error);
        _exit(-1);
    }
    real$FN_NAME = dlsym(handle, "$FN_NAME");

    if ((error = dlerror()) != NULL){
        puts(error);
        _exit(-1);
    }
    $IS_RETURN real$FN_NAME($ARG_NAME_LIST);
}

// {hold_end}
